import React, { Component } from 'react';

export default class Back extends Component {
  constructor(props) {
    super(props);
    this.state = {
      show: false,
    };
    this.back = this.back.bind(this);
  }

  static propTypes = {};

  render() {
    let { show } = this.state;
    return (
      <div onClick={this.back} className="common-back">
        {show && <i className="icon iconfont icon-huidaodingbu" />}
      </div>
    );
  }

  componentDidMount() {
    window.addEventListener(
      'scroll',
      () => {
        let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        if (scrollTop > 500) {
          this.setState({
            show: true,
          });
        } else {
          this.setState({
            show: false,
          });
        }
      },
      false,
    );
  }

  back() {
    window.scrollTo(0, 0);
  }
}
